package com.lin.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @ClassName DealJson
 * @Description 将对象变成JSON对象
 * @Author LinZePeng
 * @Date 2021/5/3 19:50
 * @Version 1.0
 */
public class DealJson {
	private static final ObjectMapper JSON=new ObjectMapper();

	/**
	 * @Description 得到一个JSON字符串
	 * @Param [obj]
	 * @return java.lang.String
	 */
	public static String getJson(Object obj){
		try {
			return JSON.writeValueAsString(obj);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @Description 将一个字符串转为一个对象
	 * @Param [str, valueType]
	 * @return java.lang.Object
	 */
	public static <T> Object getObject(String str, Class<T> valueType){
		try {
			return JSON.readValue(str,valueType);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		return null;
	}

}
